package com.bw.controller;

import com.bw.entity.PageResult;
import com.bw.entity.User;
import com.bw.entity.Word;
import com.bw.service.FinishService;
import com.bw.service.ReportService;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("finish")
public class FinishController {

    @Autowired
    private FinishService finishService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private UserService userService;

    /**
     * 查询学生列表和当天所背的单词数量是否完成
     * @param model
     * @return
     */
    @RequestMapping("queryStudent")
    @ResponseBody
    public List<User> queryStudent(ModelAndView model,String date2){
        System.out.println(date2);
        if(date2==null){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date2 = sdf.format(date);
        }
        //查询所有学生集合
        List<User> ulist = finishService.queryStudentAll();
        //遍历集合获取当天所背单词数量
        for(User user : ulist){
            Integer id = user.getId();
            Map map = new HashMap();
            map.put("id",id);
            map.put("datea",date2);
            int num = finishService.queryStudent(map);
            int num2 = reportService.findReportNum(map);
            user.setNum2(num2);
            user.setNum(num);
        }
        return ulist;
    }

    /**
     * 跳转到  查看学生已完成情况  列表
     * @return
     */
    @RequestMapping("student")
    public String StudentList(Model model){
        return "student";
    }
    /**
     * 跳转到  查看学生已背单词  列表
     * @return
     */
    @RequestMapping("word")
    public String WordList(Model model){
        return "word";
    }




    @RequestMapping("queryWords")
    public String queryWords(Integer id,String datea,Model model){
        System.out.println("------"+datea);
         //自动生成日期
        if(datea==null || datea==""){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            datea = sdf.format(date);
            System.out.println(datea);
        }
        User user = userService.findUserById(id);
        Map map = new HashMap();
        System.out.print(id);
        map.put("id",id);
        map.put("datea",datea);
        List<Word> wlist = finishService.queryWords(map);
        for(Word words : wlist){
            System.out.println(words);
        }
        model.addAttribute("wlist",wlist);
        model.addAttribute("user",user);
        return "word";
    }


}
